﻿using MediatR;
using Microsoft.IdentityModel.Tokens;
using System.Linq;
using SystemMHB.API.Application.Command.WarehouseCommand;
using SystemMHB.Domian;
using SystemMHB.Domian.DTO;
using SystemMHB.Infrstructer.Interface;

namespace SystemMHB.API.Application.CommandHandlers.WarehouseCommandHandler
{
    public class WarehouseShowCommandHandler : IRequestHandler<WarehouseShowCommand, PagingDTO<Warehouse>>
    {
        /// <summary>
        /// 仓库管理仓储
        /// </summary>
        IWarehouseRepository WarRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="WarRep">仓库管理仓储</param>
        public WarehouseShowCommandHandler(IWarehouseRepository WarRep)
        {
            this.WarRep = WarRep;
        }

        public async Task<PagingDTO<Warehouse>> Handle(WarehouseShowCommand request, CancellationToken cancellationToken)
        {
            var paging = new PagingDTO<Warehouse>();
            var quest= from war in await WarRep.GetAllAsync() where war.IsDel==false select war;
            if (!string.IsNullOrEmpty(request.warehouseNumber)) 
            {
                quest = quest.Where(p => p.warehouseNumber.Contains(request.warehouseNumber));
            }
            if (!string.IsNullOrEmpty(request.positionNumber))
            {
                quest = quest.Where(p => p.positionNumber.Contains(request.positionNumber));
            }
            if (request.warehouseState!=0)
            {
                quest = quest.Where(p => p.warehouseState== request.warehouseState);
            }
            paging.TotaCount = quest.Count();
            paging.pageCount =(int)Math.Ceiling((paging.TotaCount * 1.0 )/ request.pageSzen);
            paging.plist = quest.OrderBy(p=>p.warehouseID).Skip((request.pageiden-1)* request.pageSzen).Take(request.pageSzen).ToList();
            return paging;
        }
    }
}
